package com.czl.module_user.fragment

import android.content.Intent
import android.net.Uri
import com.alibaba.android.arouter.facade.annotation.Route
import com.annimon.stream.Collectors
import com.annimon.stream.Stream
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.AreaIdBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DayModeUtil
import com.czl.base.util.DialogHelper
import com.czl.module_user.BR
import com.czl.module_user.R
import com.czl.module_user.databinding.FragmentUserBinding
import com.czl.module_user.viewmodel.UserViewModel
import com.gyf.immersionbar.ImmersionBar

@Route(path = AppConstants.Router.User.F_USER)
class UserFragment : BaseFragment<FragmentUserBinding, UserViewModel>() {

    private lateinit var areaList: List<AreaIdBean>

    override fun initContentView(): Int {
        return R.layout.fragment_user
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun isImmersionBarEnabled(): Boolean {
        return true
    }

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .fitsSystemWindows(false)
            .statusBarDarkFont(true)
            .init()
    }

    override fun initData() {
        viewModel.getUserInfo()
    }

    override fun initViewObservable() {

        viewModel.uc.changeAreaEvent.observe(this, {
            if (areaList.isNullOrEmpty()) {
                showErrorToast("小区暂无可选")
                return@observe
            }
            DialogHelper.showBottomListDialog(
                requireContext(),
                Stream.of(areaList).map { t -> t.areaName }.withoutNulls()
                    .collect(Collectors.toList()) as ArrayList<String>
            ) { position, text ->
                viewModel.areaName.set(text)
                viewModel.model.saveAreaId(areaList!![position].areaId)
                viewModel.model.saveAreaName(text)
            }
        })

        LiveBusCenter.observeModifyUserInfoEvent(this) {
            viewModel.getUserInfo()
        }
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    private fun callPhone(phoneNum: String) {
        val intent = Intent(Intent.ACTION_DIAL)
        val data: Uri = Uri.parse("tel:$phoneNum")
        intent.data = data
        startActivity(intent)
    }

}